<?php
/**
* The sidebar menu alterations that comes with folio.
*   Essentially, it will make the sidebar menu more compact, add links to the folio
*   and activity pages.  Removes both the blog and the file sidebar components.
*
* @package folio
**/

require_once 'control/feeds.php';
require_once 'model/user.php';
require_once 'view/page.php';

/**
* Call to setup side menu page links on folio pages
* This can be a rather intrusive feature, as it replaces the default blog, file, and
*   activity links with its own ones.  See the mod/folio/config.php file for settings
*   governing its activity.
*/

/**
* Generate the text for the user activity entry
**/
function folio_sidebar_init() {
    global $CFG;
    global $FOLIO_CFG;

    // Remove unneeded links, as set by folio_cfg
    if( !empty( $FOLIO_CFG->disabled_sidemenuitems)) {
        sidebar_remove( $FOLIO_CFG->disabled_sidemenuitems );
    }


    // Grab page owner.
    $owner = folio_user::SelectWhereIdent( page_owner() );

    // Looking at a user's page.
    if( $owner != false ) {


        if( isloggedin() AND $owner->ident == $_SESSION['userid'] ) {
            // If At our page, then add current user's inbox.
            $user = folio_user::SelectWhereIdent( $_SESSION['userid'] );
            sidebar_add(15, 'folio_inbox', folio_sidebar_inbox($user) );
        } elseif(
            (isloggedin() AND $_SESSION['userid'] != $owner->ident )
            OR ( !isloggedin()  ) ) {
            if( context != 'folio_page_view' ) {
                // If we're looking at someone else, show links to their content.
                sidebar_add(15, 'folio_content', folio_sidebar_content($owner) );
            } else {
                // Show content w/o treeview control
                sidebar_add(15, 'folio_content', folio_sidebar_content($owner, false) );
            }
        }
    }

    // Show css for sidebar styling.
    global $metatags;
    $metatags .= "\n" .
        '<link type="text/css" rel="stylesheet" href="' .
        url . 'mod/folio/css/sidebar.css"/>';


}

/**
* Display the user content sidebar
* showwiki is used in cases when the sidebar is showing a page.  This keep the regular linsk from cluttering up the pages menu.
*
* @param folio_user $user The owner to show content for.
* @return html to send to sidebar_add
**/
function folio_sidebar_content( $user, $showwiki = true) {
    global $FOLIO_CFG;
    $url = url;

    if( strlen(trim($user->name)) < 1 ) {
        $name = $user->username;
    } else {
        $name = $user->name;
    }

    $hash = folio_createhash( $_SESSION['userid'] );

    $body = <<< END
        <h2>
            <a href='{$url}activity/user/{$user->username}/rss/all/all/1:{$hash}'>
            <img border=0 src='{$url}mod/folio/image/feed.png' alt='feed'/>
            </a>
            <a href='{$url}activity/user/{$user->username}/html/all/all/1'>{$name}</a>
        </h2>
        <ul>
END;

    // Profile
    if ( !in_array( 'profile', $FOLIO_CFG->disabled_foliomenuitems ) ) {
        $body .= "<li><a href='{$url}profile/extended.php?profile_name={$user->username}'>" . __gettext('Profile') . "</a></li>\n\n";
    }

    // Blog
    if ( !in_array( 'weblog', $FOLIO_CFG->disabled_foliomenuitems ) ) {
        if( $user->user_type == 'person' ) {
            $text = __gettext("Blog");
        } else {
            $text = __gettext("Community Blog");
        }

        $body .= "<li><a href='" . url . "{$user->username}/weblog/'>$text</a></li>\n\n";
    }

    // Files
    if ( !in_array( 'files', $FOLIO_CFG->disabled_foliomenuitems ) ) {
        $body .= "<li><a href=\"$url{$user->username}/files/\">" .
                __gettext('Files') . "</a></li>\n\n";
    }

    // Wiki
    if( $showwiki) {
        if ( !in_array( 'wiki', $FOLIO_CFG->disabled_foliomenuitems ) ) {
            // Contract root if the option is set for it in /mod/folio/config.php
            if ( $FOLIO_CFG->expand_page_tree) {
                $body .= folio_sidebar_wiki( $user );

            } else {
                $body .= "<li><a href=\"$url{$user->username}/page/\">" .
                    $FOLIO_CFG->page_sidebartitle . "</a></li>\n\n";
            }

        }
    }

    $body .= '</ul>';

    return $body;

}


/**
* Return the sidebar content for the wiki
*
* @param folio_user $user The owner of the page
* @param folio_page $page The root page to show
* @return string
**/

function folio_sidebar_wiki( $user, $page = false ) {
    // Add wiki entry
    global $FOLIO_CFG;
    $form = new folio_BaseView();

    // Depend upon the html/view.php page to load the metatags.
    //folio_form::LoadMetatags( folio_PageTree::metatags() );

    $body = '';

    // If no page was passsed, we're looking at the homepage instead.
    if ( !$page ) {
        $page = folio_page::SelectHomepage( $user );
    }

    if ( $page == false AND folio_page::InsertPermission( $user ) ) {
        // No homepage and the user has permission to insert a new record.
        //        Show link to the empty page.
        $body = "<li><a href='" . url . $user->username . "/page/'>" .
                    $FOLIO_CFG->page_defaulthomepage . '</a></li>';
    } elseif ( $page !== false AND $page->Permission('select') ) {
        // Page exists and we have permission.
        $form->Load( new folio_PageTree( $page ) );

        $body = '<li>' . $FOLIO_CFG->page_sidebartitle .
                '<br/>' . $form->toString() . '</li>';
    }

    return $body;
}

/**
* Return links for the user inbox.
*
* @param folio_user $user The currently logged in user
* @return string
**/
function folio_sidebar_inbox( $user ) {
    $url = url;

   /*

    // Test to see if we need to re-check the amount of activity since last log-on.
    //    If not, just use the cached values created when 1st checked this session.
    if ( !isset( $_SESSION['folio_events_all'] ) ) {


        // Grab the number of events since last logon
        $events = recordset_to_array( get_recordset_sql(
            "SELECT count(*) as rc_index, count(*) as rowcount FROM " .
            $CFG->prefix . "folio_rss ".
            "WHERE created >= {$_SESSION['folio_lastlogon']} " ) );
                if ( !$events ) {
                    // No events
                    $_SESSION['folio_events_all'] = 0;
                } else {
                    $events =  array_pop($events);
                    $_SESSION['folio_events_all'] = $events->rowcount;
        }

        // Grab the number of events in friends & joined communities since last logon
        $activity = recordset_to_array( get_recordset_sql(
            "SELECT count(*) as rc_index, count(*) as rowcount FROM " .
            $CFG->prefix . "folio_rss ".
            "WHERE created >= {$_SESSION['folio_lastlogon']} AND "  .
            folio_feeds_buildWhere( 'fac', $_SESSION['userid'], $_SESSION['userid'], array('all'), array() )
            ) );

                if ( !$activity ) {
                    // No events
                    $_SESSION['folio_events_fac'] = 0;
                } else {
                    $activity =  array_pop($activity);
                    $_SESSION['folio_events_fac'] = $activity->rowcount;
                }
    }

    // $_SESSION['folio_events_fac']    (<b>{$_SESSION['folio_events_all']} new</b>)
*/
    $oActivity = __gettext('Recent Activity');
    $oInbox = __gettext('Inbox');
    $oFAC = __gettext('Friends and Community');

    // Build the html for the side-menu to return
    $run_result = <<< END
        <h2>{$oActivity}</h2>
        <ul>
            <li><a href='{$url}activity/inbox/{$user->username}/html/all/all/1'>{$oInbox}</a></li>
            <li><a href='{$url}activity/fac/{$user->username}/summary/all/all/1'>{$oFAC}</a></li>
        </ul>
END;

    return $run_result;
}

?>
